<template>
    <div>
      <el-form :model="form" label-width="120px">
        <el-form-item label="用户名">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="register">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </template>
  
  <script>
  import { ref } from 'vue';
  import axios from 'axios';
  import { useRouter } from 'vue-router';
  
  export default {
    setup() {
      const form = ref({
        username: '',
        password: ''
      });
      const router = useRouter();
  
      const register = async () => {
        try {
          const response = await axios.post('/api/register', form.value);
          if (response.data.code === 200) {
            ElMessage.success('注册成功');
            router.push('/signin');
          } else {
            ElMessage.error(response.data.message);
          }
        } catch (error) {
          ElMessage.success('网络错误');
        }
      };
  
      return {
        form,
        register
      };
    }
  };
  </script>
  